home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / extra / console.c next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  114 lines

  1.  
  2. /*
  3.  *  CONSOLE.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  *
  9.  *  OpenConsole()   - set this process's console as far as we can do such
  10.  *              things.
  11.  */
  12.  
  13. #define DOSBase_DECLARED
  14.  
  15. #include <exec/types.h>
  16. #include <exec/nodes.h>
  17. #include <exec/ports.h>
  18. #include <exec/alerts.h>
  19. #include <dos/dos.h>
  20. #include <dos/dosextens.h>
  21. #include <dos/filehandler.h>
  22. #include <clib/dos_protos.h>
  23. #include <clib/exec_protos.h>
  24. #include <clib/alib_protos.h>
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <lib/misc.h>
  30.  
  31. #define BTOC(bptr)  ((void *)((long)(bptr) << 2))
  32. #define CTOB(cptr)  ((BPTR)(((long)cptr) >> 2))
  33.  
  34. #define DOS_TRUE    (-1)
  35. #define DOS_FALSE   (0)
  36.  
  37. #ifdef DEBUG
  38. #define dbprintf(x) fhprintf x
  39. #else
  40. #define dbprintf(x)
  41. #endif
  42.  
  43. typedef struct FileHandle   FileHandle;
  44. typedef struct Process        Process;
  45. typedef struct List        List;
  46. typedef struct MsgPort        MsgPort;
  47. typedef struct Message        Message;
  48. typedef struct CommandLineInterface CLI;
  49. typedef struct Task        Task;
  50.  
  51. static BPTR  CustomCIS;
  52. static BPTR  CustomCOS;
  53. static BPTR  SaveCIS;
  54. static BPTR  SaveCOS;
  55. static MsgPort *SaveConsoleTask;
  56.  
  57. extern struct DosLibrary *DOSBase;
  58.  
  59. __autoexit static
  60. void
  61. opencon_exit(void)
  62. {
  63.     Process *proc = (Process *)FindTask(NULL);
  64.  
  65.     if (CustomCIS || CustomCOS)
  66.     proc->pr_ConsoleTask = SaveConsoleTask;
  67.     if (CustomCIS) {
  68.     proc->pr_CIS = SaveCIS;
  69.     Close(CustomCIS);
  70.     CustomCIS = 0;
  71.     }
  72.     if (CustomCOS) {
  73.     Write(CustomCOS, "**END**\n", 8);
  74.     proc->pr_COS = SaveCOS;
  75.     Close(CustomCOS);
  76.     CustomCOS = 0;
  77.     }
  78. }
  79.  
  80. BOOL
  81. OpenConsole(str)
  82. const char *str;
  83. {
  84.     Process *proc = (Process *)FindTask(NULL);
  85.     FileHandle *fh;
  86.     BOOL r = FALSE;
  87.  
  88.     opencon_exit();
  89.     if (CustomCIS = Open(str, 1005)) {
  90.     fh = BTOC(CustomCIS);
  91.     if (fh->fh_Type) {
  92.         r = TRUE;
  93.  
  94.         SaveConsoleTask = proc->pr_ConsoleTask;
  95.         SaveCOS = proc->pr_COS;
  96.         SaveCIS = proc->pr_CIS;
  97.         proc->pr_ConsoleTask = fh->fh_Type;
  98.         proc->pr_COS = CustomCOS = Open("*", 1005);
  99.         proc->pr_CIS = CustomCIS;
  100.         freopen("*", "r", stdin);
  101.         freopen("*", "w", stdout);
  102.         freopen("*", "w", stderr);
  103.         /*proc->pr_ConsoleTask = SaveConsoleTask;*/
  104.         stdout->sd_Flags |= __SIF_IOLBF;
  105.         stderr->sd_Flags |= __SIF_IOLBF;
  106.     } else {
  107.         Close(CustomCIS);
  108.         CustomCIS = 0;
  109.     }
  110.     }
  111.     return(r);
  112. }
  113.  
  114.